package com.wbk.lotterysystemsystemservice.service.serviceImpl;

import cn.hutool.crypto.SecureUtil;
import com.wbk.lotterysystemsystemservice.common.bo.AdminLoginBO;

import com.wbk.lotterysystemsystemservice.entity.SysUser;
import com.wbk.lotterysystemsystemservice.entity.SysUserExample;
import com.wbk.lotterysystemsystemservice.mapper.SysUserMapper;
import com.wbk.lotterysystemsystemservice.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    SysUserMapper sysUserMapper;

    @Cacheable(cacheNames = "sysUser::login", key = "#adminLoginBO.username+'-'+#adminLoginBO.password")
    @Override
    public SysUser getUser(AdminLoginBO adminLoginBO) {
        SysUserExample sysUserExample = new SysUserExample();
        SysUserExample.Criteria criteria = sysUserExample.createCriteria();
        criteria.andUsernameEqualTo(adminLoginBO.getUsername());
        criteria.andPasswordEqualTo(SecureUtil.md5(adminLoginBO.getPassword()));
        List<SysUser> sysUsers = sysUserMapper.selectByExample(sysUserExample);
        if (sysUsers.isEmpty()) {
            return null;
        }
        return sysUsers.get(0);
    }

    @Cacheable(cacheNames = "sysUser::byUserNo", key = "#userNo")
    @Override
    public SysUser getUserByUserNo(Long userNo) {
        SysUserExample sysUserExample = new SysUserExample();
        SysUserExample.Criteria criteria = sysUserExample.createCriteria();
        criteria.andUserNoEqualTo(userNo);
        List<SysUser> sysUsers = sysUserMapper.selectByExample(sysUserExample);
        if (sysUsers.isEmpty()) {
            return null;
        }
        return sysUsers.get(0);
    }
}
